package yxy.game.pm2.command.office;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.conscriptldate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.OfficeMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.office.OfficeHandle;
import yxy.game.pm2.module.office.OfficeModule;
import yxy.game.pm2.module.resonate.ResonateHandle;

/** 主公府 主公府武将置换 */
@MessageType(proto = Command.OfficeHeroChange, classtype = OfficeMessage.OfficeHeroChangeRequest.class)
public class CMD_OfficeHeroChange extends Command<OfficeMessage.OfficeHeroChangeRequest> {

	@Override
	public void execute(MessageSource messageSource, OfficeMessage.OfficeHeroChangeRequest request) {
		String playerCode = messageSource.getTag();
		String code = request.getHero();

		OfficeMessage.OfficeHeroChangeResponse.Builder response = OfficeMessage.OfficeHeroChangeResponse.newBuilder();

		OfficeModule officeModule = OfficeModule.get(playerCode);
		if (officeModule == null) {
			send(messageSource, response, Result.ModuleNotOpen);// 模块未开启
			return;
		}
		try {
			if (officeModule.tryLock()) {
				if (officeModule.getChangeHeroCode() != null && !officeModule.getChangeHeroCode().equals(code)) {
					send(messageSource, response, Result.ParamError);
					return;
				}

				HeroHandle heroHandle = new HeroHandle();
				Hero hero = heroHandle.getHero(playerCode, code);
				if (hero == null || hero.getStar() < 4 || hero.getStar() > 5) {
					send(messageSource, response, Result.ParamError);
					return;
				}

				OfficeHandle officeHandle = new OfficeHandle();
				int change = officeHandle.change(playerCode, hero);
				if (change <= 0) {
					send(messageSource, response, Result.DataError);
					return;
				}

				//共鸣的武将不能置换
				Result resultForResonate = ResonateHandle.heroIsResonateHero(hero, playerCode);
				if(!resultForResonate.equals(Result.Success)){
					send(messageSource, response, resultForResonate);
					return;
				}

				conscriptldate costdata = DataCenter.getData(2, conscriptldate.class);
				CurrencyHandle currencyHandle = new CurrencyHandle();
				Currency cost = Currency.create(costdata.getCosttype1(), costdata.getCostid1(), costdata.getCostnum1());
				try {
					currencyHandle.cost(playerCode, CCReason.主公府武将置换, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}

				officeModule.setChangeHeroCode(code);
				officeModule.setTargetHeroId(change);
				officeModule.set();

				response.setTarget(officeModule.getTargetHeroId());
				send(messageSource, response, Result.Success);

				//判断如果消耗的是共鸣系统场上的武将，对应操作
				ResonateHandle.heroIsAboveHeroAndResolve(hero, playerCode);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			officeModule.unlock();
		}
	}

	private void send(MessageSource messageSource, OfficeMessage.OfficeHeroChangeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}